public class Accountattachmentcontroller
{
	public List<OpportunityAttachmentWper> lstAttachment{get;set;}
	public string AccountId{get;set;}
	public Accountattachmentcontroller(ApexPages.StandardController stdController)
	{
 		lstAttachment=new List<OpportunityAttachmentWper>();
  		Set<Id> setIds=new set<Id>();
 		Account acc=(Account)stdController.getRecord();
 		AccountId=acc.Id;
 		setIds.add(AccountId);
 		Set<Id> setaccid=new Set<Id>();
		List<Account> Lstaccount;
 		Account masteracc=[select id,Master_Client__c,RecordType.Name from Account where id=:AccountId];
		if(masteracc.RecordType.Name=='AAS Sales Account')
		{
    		if(masteracc.Master_Client__c!=null)
    		{
    			Account Mstaccount=[Select id from Account where Id=:masteracc.Master_Client__c];
    			setIds.add(Mstaccount.Id);
    			setaccid.add(AccountId);
    		}
		}
		else if(masteracc.RecordType.Name=='AAS Master Client')
		{
    		Lstaccount=[Select id from Account where Master_Client__c=:AccountId];
    	if(Lstaccount.size()!=0)
    	{
    		for(Account accnt:Lstaccount)
    		{
    			setaccid.add(accnt.Id);
    			setIds.add(accnt.Id);
    		}
    	}
    	else
    	{
    		setaccid.add(AccountId);
    	}
	}
	List<Opportunity> lstOpportunity=[Select id from Opportunity  where AccountId in:setaccid];
	for(Opportunity opp:lstOpportunity)
	{
	setIds.add(opp.Id);
	}
 	List<Quote> LstQuote=[select Id from Quote where OpportunityId in :lstOpportunity];
	for(Quote Quote1:LstQuote)
	{
		setIds.add(Quote1.Id);
	}
	List<Order__c> lstOrder=[select id from Order__c where Opportunity__c in :lstOpportunity]; 
	for(Order__c Order:lstOrder)
	{
		setIds.add(Order.Id);
	}
	List<Attachment> lstAttachments=[Select Id,ParentID,LastModifiedDate,CreatedById,LastModifiedById,Parent.Type,Parent.Name,Name,LastModifiedBy.Name,CreatedBy.Name,ContentType From Attachment where ParentId in :setIds];
 	for(Attachment attch:lstAttachments)
	{
		OpportunityAttachmentWper attachwper=new OpportunityAttachmentWper();
		attachwper.attachmentId=attch.Id;
		attachwper.ParentID=attch.ParentId;
		if(attch.Parent.Type=='Order__c')
			attachwper.Objects ='Fulfillment Order';
		else
			attachwper.Objects=attch.Parent.Type;
		attachwper.objtype=attch.Parent.Name;
		attachwper.Title=attch.Name;
		attachwper.CreatedByID=attch.CreatedById;
		attachwper.LastModifiedDate=String.valueOf(attch.LastModifiedDate);
		attachwper.LastModifiedID=attch.LastModifiedById;
		attachwper.LastModifiedBy=attch.LastModifiedBy.Name;
		attachwper.CreatedBy=attch.CreatedBy.Name;
		lstAttachment.add(attachwper);
	}
}
	public String getParam(String name) 
	{
		return ApexPages.currentPage().getParameters().get(name);   
	}
	public PageReference del() 
	{	      
        try 
        {
        	String delid = getParam('delid');
        	Integer rowNo =Integer.valueOf(getParam('rowNo'));     
        	lstAttachment.remove(rowNo);    
        	if(delid.startsWith('00T'))
        	{
        		Attachment attach=[select id from Attachment where id=:delid];
        		delete attach;
        	}            
        } catch (Exception e) {
            ApexPages.addMessages(e);
        }
        return null;
	}
 	public PageReference edit()
	{
		String editid = getParam('editid');
        PageReference pageref=new PageReference('/'+editid+'/e');
        pageref.setredirect(true);
        return pageref;
	} 
}